// https://github.com/TypeStrong/ts-loader/issues/653#issuecomment-390889335
import webpack, { Compiler } from 'webpack'
// @ts-expect-error
import ModuleDependencyWarning from 'webpack/lib/ModuleDependencyWarning'

export class IgnoreNotFoundExportPlugin {
  messageRegExp = /export '.*'( \((imported|reexported) as '.*'\))? was not found in/

  doneHook = (compilation: webpack.Compilation) => {
    compilation.warnings = compilation.warnings.filter((warn) => {
      return !(
        (warn instanceof ModuleDependencyWarning || warn instanceof Error) &&
        this.messageRegExp.test(warn.message)
      )
    })
  }

  apply(compiler: Compiler) {
    compiler.hooks.emit.tap('IgnoreNotFoundExportPlugin', this.doneHook)
  }
}
